package com.helios.user.service;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.helios.core.service.ServiceAdapter;
import com.helios.user.bean.Manager;
import com.helios.user.mapper.ManagerMapper;

@Service
public class ManagerService extends ServiceAdapter<Manager> {
	
	private Logger logger = Logger.getLogger(getClass());
    
    @Autowired
    private ManagerMapper mapper;
    
    @Value("#{configProperties.bean_manager_time}")
    private int cacheTime;

    @Override
    protected String getDispInfo() {
        return "管理员信息";
    }

    @Override
    protected String getCacheKey(Object args) {
        if(args instanceof Manager) {
            return "MANAGER_" + ((Manager) args).getId();
        } else {
            return "MANAGER_" + args.toString();
        }
    }

    @Override
    protected int getCacheTime() {
        return cacheTime;
    }
    
    public Manager login(String username, String password){
        final String md5Pwd = DigestUtils.md5DigestAsHex(password.getBytes());
        return getCache(username, Manager.class, (key) -> mapper.get(key.toString(), md5Pwd));
    }
    
    public List<Manager> gets(Integer page, Integer rows) {
    	try {
    		int index = (page - 1) * rows;
            if (index < 0) index = 0;
    		return mapper.gets(index, rows);
    	} catch (Exception e) {
    		logger.error(String.format("获取[%s列表]异常,异常原因:%s", getDispInfo(), e.getMessage()));
    		return null;
    	}
    }
    
    public int getCount() {
    	try {
    		return mapper.getCount();
    	} catch(Exception e) {
    		logger.error(String.format("获取[%s个数]异常,异常原因:%s", getDispInfo(), e.getMessage()));
    		return 0;
    	}
    }
}
